package com.tonann.app.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.tonann.app.domain.AppDevice;
import com.tonann.app.domain.AppDeviceModel;
import com.tonann.app.dto.AppDeviceDocumentDto;
import com.tonann.app.service.IAppDeviceModelService;
import com.tonann.app.service.IAppDeviceService;
import com.tonann.common.utils.DateUtils;
import com.tonann.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tonann.app.mapper.AppDeviceDocumentMapper;
import com.tonann.app.domain.AppDeviceDocument;
import com.tonann.app.service.IAppDeviceDocumentService;

import javax.annotation.Resource;

/**
 * 设备文档管理Service业务层处理
 * 
 * @author tonann
 * @date 2023-07-28
 */
@Service
public class AppDeviceDocumentServiceImpl implements IAppDeviceDocumentService 
{
    @Resource
    private AppDeviceDocumentMapper appDeviceDocumentMapper;

    @Resource
    private IAppDeviceModelService appDeviceModelService;

    /**
     * 查询设备文档管理
     * 
     * @param documentId 设备文档管理主键
     * @return 设备文档管理
     */
    @Override
    public AppDeviceDocument selectAppDeviceDocumentByDocumentId(Long documentId)
    {
        return appDeviceDocumentMapper.selectAppDeviceDocumentByDocumentId(documentId);
    }

    /**
     * 查询设备文档管理列表
     * 
     * @param appDeviceDocument 设备文档管理
     * @return 设备文档管理
     */
    @Override
    public List<AppDeviceDocument> selectAppDeviceDocumentList(AppDeviceDocument appDeviceDocument)
    {

        List<AppDeviceDocument> list = appDeviceDocumentMapper.selectAppDeviceDocumentList(appDeviceDocument);
        List<AppDeviceDocument> vo = new ArrayList<>();
        for(AppDeviceDocument deviceDocument : list){
            AppDeviceModel deviceModel = appDeviceModelService.selectAppDeviceModelByModelId(deviceDocument.getModelId());
            deviceDocument.setDeviceModel(deviceModel);
            vo.add(deviceDocument);
        }
        return vo;
    }

    /**
     * 新增设备文档管理
     * 
     * @param appDeviceDocument 设备文档管理
     * @return 结果
     */
    @Override
    public int insertAppDeviceDocument(AppDeviceDocumentDto appDeviceDocument)
    {

        Long modelId = appDeviceDocument.getModelId();
        AppDeviceModel deviceModel = appDeviceModelService.selectAppDeviceModelByModelId(modelId);
        if (deviceModel != null){
            for(AppDeviceDocument deviceDocument : appDeviceDocument.getFileList()){
                deviceDocument.setModelId(modelId);
                deviceDocument.setModelCode(deviceModel.getModelCode());
                deviceDocument.setCreateTime(new Date());
                deviceDocument.setCreateBy(SecurityUtils.getUsername());
                deviceDocument.setUpdateTime(new Date());
                deviceDocument.setUpdateBy(SecurityUtils.getUsername());
                deviceDocument.setCreateBy(SecurityUtils.getUsername());
                deviceDocument.setCreateTime(DateUtils.getNowDate());
                deviceDocument.setUpdateBy(SecurityUtils.getUsername());
                deviceDocument.setUpdateTime(DateUtils.getNowDate());
                appDeviceDocumentMapper.insertAppDeviceDocument(deviceDocument);
            }
        }
        return 1;
    }

    /**
     * 修改设备文档管理
     * 
     * @param appDeviceDocument 设备文档管理
     * @return 结果
     */
    @Override
    public int updateAppDeviceDocument(AppDeviceDocument appDeviceDocument)
    {
        appDeviceDocument.setUpdateBy(SecurityUtils.getUsername());
        appDeviceDocument.setUpdateTime(DateUtils.getNowDate());
        return appDeviceDocumentMapper.updateAppDeviceDocument(appDeviceDocument);
    }

    /**
     * 批量删除设备文档管理
     * 
     * @param documentIds 需要删除的设备文档管理主键
     * @return 结果
     */
    @Override
    public int deleteAppDeviceDocumentByDocumentIds(Long[] documentIds)
    {
        return appDeviceDocumentMapper.deleteAppDeviceDocumentByDocumentIds(documentIds);
    }

    /**
     * 删除设备文档管理信息
     * 
     * @param documentId 设备文档管理主键
     * @return 结果
     */
    @Override
    public int deleteAppDeviceDocumentByDocumentId(Long documentId)
    {
        return appDeviceDocumentMapper.deleteAppDeviceDocumentByDocumentId(documentId);
    }
}
